﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

         Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            socket.Connect("192.168.1.97", 10001);
            Task.Run(() => Recive());
        }

        void Recive()
        {
            while (true)
            {
                if (socket.Connected)
                {
                    var buff = new byte[20];
                    var len= socket.Receive(buff);
                    //var rcvStr=Encoding.Default.GetString(buff, 0, len);
                    Exp(buff);
                    //Console.WriteLine(rcvStr);
                }
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (socket.Connected)
            {
                socket.Send(Encoding.Default.GetBytes("Z"));
            }
        }

        void Exp(byte[] buff)
        {
            var section1 = Encoding.Default.GetString(buff, 0, 2);
            var section2 = Encoding.Default.GetString(buff, 3, 2);
            var section3 = Encoding.Default.GetString(buff, 6, 1);
            var section4 = Encoding.Default.GetString(buff, 7, 7);
            var section5 = Encoding.Default.GetString(buff, 15, 2);
            Console.WriteLine("{0} {1} {2} {3} {4}", section1, section2, section3, section4, section5);
        }
    }
}
